<!--
ipAddr - An IP utility knife for javascript
Copyright (C) 2010 Clif Bratcher

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
-->
<html>
<head>
<script type='text/javascript' src='js/ipAddr.js'></script>
<script type='text/javascript' src='../jsunit/app/jsUnitCore.js'></script>
<style type='text/css'>
table.info {
  width: 640;
  border-collapse: collapse;
  border-width: 1px;
  border-spacing: 1px;
  border-style: solid;
  border-color: black;
}

table.info td {
  border-width: 1px;
  padding: 2px;
  border-style: solid;
  border-color: black;
}

input.text {
    border-style: solid;
    border-width: 1pt;
}
</style>

</head>
<body>
<script language="JavaScript">

function Looper (tests, bool, version) {
  for (var i=0; i < tests.length; i++) {
    var addr = tests[i];
    var ip = new ipAddr(addr);
    assertEquals(addr, version, ip.Version());
    if (bool)
      assertTrue(addr, ip.Valid());
    else
      assertFalse(addr, ip.Valid());
  }
}

function testIPv6Netmask () {
  var tests = ['0000:0000:0000:0000:0000:0000:0000:0000',
               '8000:0000:0000:0000:0000:0000:0000:0000',
               'C000:0000:0000:0000:0000:0000:0000:0000',
               'E000:0000:0000:0000:0000:0000:0000:0000',
               'F000:0000:0000:0000:0000:0000:0000:0000',
               'F800:0000:0000:0000:0000:0000:0000:0000',
               'FC00:0000:0000:0000:0000:0000:0000:0000',
               'FE00:0000:0000:0000:0000:0000:0000:0000',
               'FF00:0000:0000:0000:0000:0000:0000:0000',
               'FF80:0000:0000:0000:0000:0000:0000:0000',
               'FFC0:0000:0000:0000:0000:0000:0000:0000',
               'FFE0:0000:0000:0000:0000:0000:0000:0000',
               'FFF0:0000:0000:0000:0000:0000:0000:0000',
               'FFF8:0000:0000:0000:0000:0000:0000:0000',
               'FFFC:0000:0000:0000:0000:0000:0000:0000',
               'FFFE:0000:0000:0000:0000:0000:0000:0000',
               'FFFF:0000:0000:0000:0000:0000:0000:0000',
               'FFFF:8000:0000:0000:0000:0000:0000:0000',
               'FFFF:C000:0000:0000:0000:0000:0000:0000',
               'FFFF:E000:0000:0000:0000:0000:0000:0000',
               'FFFF:F000:0000:0000:0000:0000:0000:0000',
               'FFFF:F800:0000:0000:0000:0000:0000:0000',
               'FFFF:FC00:0000:0000:0000:0000:0000:0000',
               'FFFF:FE00:0000:0000:0000:0000:0000:0000',
               'FFFF:FF00:0000:0000:0000:0000:0000:0000',
               'FFFF:FF80:0000:0000:0000:0000:0000:0000',
               'FFFF:FFC0:0000:0000:0000:0000:0000:0000',
               'FFFF:FFE0:0000:0000:0000:0000:0000:0000',
               'FFFF:FFF0:0000:0000:0000:0000:0000:0000',
               'FFFF:FFF8:0000:0000:0000:0000:0000:0000',
               'FFFF:FFFC:0000:0000:0000:0000:0000:0000',
               'FFFF:FFFE:0000:0000:0000:0000:0000:0000',
               'FFFF:FFFF:0000:0000:0000:0000:0000:0000',
               'FFFF:FFFF:8000:0000:0000:0000:0000:0000',
               'FFFF:FFFF:C000:0000:0000:0000:0000:0000',
               'FFFF:FFFF:E000:0000:0000:0000:0000:0000',
               'FFFF:FFFF:F000:0000:0000:0000:0000:0000',
               'FFFF:FFFF:F800:0000:0000:0000:0000:0000',
               'FFFF:FFFF:FC00:0000:0000:0000:0000:0000',
               'FFFF:FFFF:FE00:0000:0000:0000:0000:0000',
               'FFFF:FFFF:FF00:0000:0000:0000:0000:0000',
               'FFFF:FFFF:FF80:0000:0000:0000:0000:0000',
               'FFFF:FFFF:FFC0:0000:0000:0000:0000:0000',
               'FFFF:FFFF:FFE0:0000:0000:0000:0000:0000',
               'FFFF:FFFF:FFF0:0000:0000:0000:0000:0000',
               'FFFF:FFFF:FFF8:0000:0000:0000:0000:0000',
               'FFFF:FFFF:FFFC:0000:0000:0000:0000:0000',
               'FFFF:FFFF:FFFE:0000:0000:0000:0000:0000',
               'FFFF:FFFF:FFFF:0000:0000:0000:0000:0000',
               'FFFF:FFFF:FFFF:8000:0000:0000:0000:0000',
               'FFFF:FFFF:FFFF:C000:0000:0000:0000:0000',
               'FFFF:FFFF:FFFF:E000:0000:0000:0000:0000',
               'FFFF:FFFF:FFFF:F000:0000:0000:0000:0000',
               'FFFF:FFFF:FFFF:F800:0000:0000:0000:0000',
               'FFFF:FFFF:FFFF:FC00:0000:0000:0000:0000',
               'FFFF:FFFF:FFFF:FE00:0000:0000:0000:0000',
               'FFFF:FFFF:FFFF:FF00:0000:0000:0000:0000',
               'FFFF:FFFF:FFFF:FF80:0000:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFC0:0000:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFE0:0000:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFF0:0000:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFF8:0000:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFFC:0000:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFFE:0000:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:0000:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:8000:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:C000:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:E000:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:F000:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:F800:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FC00:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FE00:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FF00:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FF80:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFC0:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFE0:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFF0:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFF8:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFC:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFE:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:0000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:8000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:C000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:E000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:F000:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:F800:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FC00:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FE00:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FF00:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FF80:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFC0:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFE0:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFF0:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFF8:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFC:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFE:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:0000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:8000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:C000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:E000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:F000:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:F800:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FC00:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FE00:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FF00:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FF80:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFC0:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFE0:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFF0:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFF8:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFC:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFE:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:0000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:8000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:C000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:E000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:F000',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:F800',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FC00',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FE00',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FF00',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FF80',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFC0',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFE0',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFF0',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFF8',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFC',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFE',
               'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF'];

    for (var i=0; i <= 128; i++) {
        var ip = new ipAddr('2000::1', i);
        assertEquals("V6 CIDR "+i, tests[i], ip.Netmask());
    }
}

function testIPv4Netmask () {
  var tests = ['0.0.0.0','128.0.0.0','192.0.0.0','224.0.0.0','240.0.0.0',
               '248.0.0.0','252.0.0.0','254.0.0.0','255.0.0.0','255.128.0.0',
               '255.192.0.0','255.224.0.0','255.240.0.0','255.248.0.0',
               '255.252.0.0','255.254.0.0','255.255.0.0','255.255.128.0',
               '255.255.192.0','255.255.224.0','255.255.240.0',
               '255.255.248.0','255.255.252.0','255.255.254.0',
               '255.255.255.0','255.255.255.128','255.255.255.192',
               '255.255.255.224','255.255.255.240','255.255.255.248',
               '255.255.255.252','255.255.255.254','255.255.255.255'];

    for (var i=0; i <= 32; i++) {
        var ip = new ipAddr('4.3.2.1', i);
        assertEquals("V4 CIDR "+i, tests[i], ip.Netmask());
    }
}

function testIPv4Hosts () {
  var tests = ['4294967296','2147483648','1073741824','536870912','268435456',
               '134217728','67108864','33554432','16777216','8388608',
               '4194304','2097152','1048576','524288','262144','131072',
               '65536','32768','16384','8192','4096','2048','1024','512',
               '256','128','64','32','16','8','4','2','1'];

    for (var i=0; i <= 32; i++) {
        var ip = new ipAddr('4.3.2.1', i);
        assertEquals("V4 CIDR "+i, tests[i], ip.HostCount());
    }
}

function testIPv4CidrRange () {
  var tests = 300;
  while(tests--) {
    var cidr = Math.floor(Math.random()*300) - Math.floor(Math.random()*150);

    var ip = new ipAddr('187.2.3.4', cidr);
    var ip2 = new ipAddr('54.32.10.1/' + cidr);

    if (cidr < 0 || 32 < cidr)
      assertFalse("CIDR: " + cidr, ip.ValidCidr());
    else
      assertTrue("CIDR:" + cidr, ip.ValidCidr());

    if (cidr < 0 || 32 < cidr)
      assertFalse("CIDR: " + cidr, ip2.ValidCidr());
    else
      assertTrue("CIDR: " + cidr, ip2.ValidCidr());
  }
}


function testIPv6CidrRange () {
  var tests = 300;
  while(tests--) {
    var cidr = Math.floor(Math.random()*500) - Math.floor(Math.random()*250);

    var ip = new ipAddr('2001::', cidr);
    var ip2 = new ipAddr('2001::/' + cidr);

    if (cidr < 0 || 128 < cidr)
      assertFalse("CIDR: " + cidr, ip.ValidCidr());
    else
      assertTrue("CIDR:" + cidr, ip.ValidCidr());

    if (cidr < 0 || 128 < cidr)
      assertFalse("CIDR: " + cidr, ip2.ValidCidr());
    else
      assertTrue("CIDR:" + cidr, ip2.ValidCidr());
  }
}

function testTotallyInvalid () {
  var tests = [
    '1',
    '1.2',
    '1.2.3',
    'bad'];

  Looper(tests, false, 0);

}

function testInvalidIPv4 () {
  var tests = [
    '1.2.3.4.5',
    '256.256.256.256',
    'q.1.2.3',
    '1.2.3.400',
    'a.b.c.d'];

  Looper(tests, false, 4);
}

function testValidIPv4 () {

  var tests = [
    '0.0.0.0',
    '10.0.0.0',
    '169.254.0.0',
    '172.16.0.0',
    '192.168.0.0',
    '127.0.0.1',
    '255.255.255.255',
    '254.0.0.0',
    '254.255.255.255'];

  Looper(tests, true, 4);
}

function testInvalidIPv6 () {
  var tests = [
    '1:2:3:4:quux',
    '12::34::56',
    ':::',
    '0:0:0:0:0:0:0:0:0',
    'FF01: 0: 0: 0: 0: 0: 0: :101',
    '1.2.3.4:FFFF::',
    '::FFFF:192.168.0.1:0',
    ':0000:0000:0000:0000:0000:1',
    '0000:0000:0000:0000:0000:0000:0000:',
    '::192.168.0.1',
    ':',
    '::00000',
    '00000::',
    '00000::00000',
    '0:0',
    '0:0:0',
    '0:0:0:0',
    '0:0:0:0:0',
    '0:0:0:0:0:0',
    '0:0:0:0:0:0:0',
    '0:0:0:0:0:0:0:',
    '0:0:0:0:0:0:0:0:'];


  Looper(tests, false, 6);
}

function testValidIPv6 () {
  var tests = [
    '::1',
    '0:0:0:0:0:0:0:1',
    '::',
    '0:0:0:0:0:0:0:0',
    '::FFFF',
    '::FFFF:FFFF',
    '::FFFF:FFFF:FFFF',
    '::FFFF:FFFF:FFFF:FFFF',
    '::FFFF:FFFF:FFFF:FFFF:FFFF',
    '::FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
    '::FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
    'FFFF::',
    'FFFF:FFFF::',
    'FFFF:FFFF:FFFF::',
    'FFFF:FFFF:FFFF:FFFF::',
    'FFFF:FFFF:FFFF:FFFF:FFFF::',
    'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF::',
    'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF::',
    'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
    '2001:DB8::8:800:200C:417A',
    '2001:DB8:0:0:8:800:200C:417A',
    'FF01::101',
    'FF01:0:0:0:0:0:0:101',
    '::FFFF:192.168.0.1',
    '::0000:0000:0000:0000:0000:1'];

  Looper(tests, true, 6);
}

function testAssertion () {
  assert(true);
}
</script>
<tt>
Run me through jsUnit!
</tt>
</body>
</html>
